﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 图片裁剪压缩工具
{
    public class ImageDisplayInfo
    {
        public Rectangle Rectangle { set; get; }
        public double K { set; get; }
        private ImageDisplayInfo(PictureBox pictureBox)
        {
            int iw = pictureBox.Image.Width;
            int ih = pictureBox.Image.Height;
            int pw = pictureBox.Width;
            int ph = pictureBox.Height;
            if (ih * pw > ph * iw)
            {
                K = (double)ih / ph;
                int sw = (int)(iw / K);
                int sh = ph;
                Rectangle = new Rectangle((pw - sw) / 2, 0, sw, sh);
            }
            else
            {
                K = (double)iw / pw;
                int sw = pw;
                int sh = (int)(ih / K);
                Rectangle = new Rectangle(0, (ph - sh) / 2, sw, sh);
            }

        }
        public static ImageDisplayInfo GetInstance(PictureBox pictureBox)
        {
            return (pictureBox.Image != null && pictureBox.SizeMode == PictureBoxSizeMode.Zoom) ? new ImageDisplayInfo(pictureBox) : null;
        }
    }
}
